﻿@using System.Web.Script.Serialization
@model IEnumerable<BusinessLogic.DTO.PositionDTO>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_adminLayout.cshtml";
}

<script type="text/javascript">
    var initialData = @Html.Raw(new JavaScriptSerializer().Serialize(Model));
    
    function changeRecruitmentStatus(parameter) {
        var id = parameter.id;
        $.post("/Position/Update", { "id": id },
                   function (data) {             
                   })
            .done(function() {
                var elem = document.getElementById("status");
                elem.innerHTML = 'Saved to database';
                elem.style.color = "Green";
            $('#status').fadeIn().delay(10000).fadeOut(); 
            })
            .fail(function() {  document.getElementById('status').innerHTML  = 'Error while saving to database' });
    }
    
    $(function () {
        $(".RemoveLink").click(function () {
            var recordToDelete = $(this).attr("data-id");
            if (recordToDelete != '') {
                $.post("/Position/Remove", { "id": recordToDelete },
                    function (data) {
                        $('#row-' + data.DeleteId).fadeOut('slow');
                        $('#update-message').text(data.Message);
                        if(data.ItemCount == 0)
                            $('#positionTable').fadeOut('slow');
                                
                    });
            }
        });
    });
</script>

<h2>Zarządzaj stanowiskami</h2>

<div id="update-message">
</div>

<table class="hovered" id="positionTable">
    <tr>
        <th>
            ID
        </th>
        <th>
            Stanowisko
        </th>
        <th>
            Kategorie pytań
        </th>
        <th>
            Poziom trudności
        </th>
        <th>
            Aktywne
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model) {
        <tr id="row-@item.Position.PositionID">
            <td>
                @Html.DisplayFor(modelItem => item.Position.PositionID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Position.PositionName)
            </td>
            <td>
                @foreach(var cat in item.CategoryToPosition){
                    <p>
                        @Html.DisplayFor(modelItem => cat.Category.Name)
                    </p>}
            </td>
            <td>
                @foreach(var lvl in item.QuestionDifficultyToPosition){
                    <p>
                        @Html.DisplayFor(modelItem => lvl.Difficulty.DifficultyName)
                        @Html.Raw("(" + lvl.Quantity+")")
                    </p>
                }
            </td>
            <td style="text-align: center">
                @Html.CheckBoxFor(modelItem => item.Position.Recruitment,new { onClick="changeRecruitmentStatus(this);", id = item.Position.PositionID })
            </td>
            <td>
                @Html.ActionLink("Edycja", "Edit", new { id=item.Position.PositionID }) | 
                <a href="#" class="RemoveLink" data-id="@item.Position.PositionID">Usuń</a>
            </td>
        </tr>
    }

</table>

<p style="float: right; margin-right: 15px" id="status"></p>
